package com.micro.download;

import com.micro.entity.TaskInfo;
import com.micro.listener.HttpInfoListener;
import com.micro.utils.HttpUtil;

import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author dufen
 * @date 2021-10-29 10:44:55
 * @description
 */
public class DownloadBilibili extends ThreadPoolExecutor implements Runnable {
    private TaskInfo taskInfo;
    private long accept_quality;
    public DownloadBilibili(TaskInfo taskInfo, long accept_quality) {
        super(1, 1, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), new MyThreadFactory("Bilibili-Task-Group", "BiliBili-Task-Thread"));
        this.taskInfo = taskInfo;
        this.accept_quality =accept_quality;
    }

    @Override
    public void run() {
        start();
    }


    public void start() {
        downloadTask();
    }

    public void downloadTask() {
        String baseUrl = taskInfo.getBilibiliData().getDash().getVideo().stream().filter(video -> video.getId() == accept_quality).map(video -> video.getBaseUrl()).findFirst().get();
        String audioUrl = taskInfo.getBilibiliData().getDash().getAudio().get(0).getBaseUrl();
        execute(() -> {
            try {
                HttpUtil.httpGetDownload(baseUrl, taskInfo.getRequestHeader().getHeaders(), taskInfo.getCachePath() +"/"+ taskInfo.getName()+".m4s", 0, new HttpInfoListener() {

                    @Override
                    public void setSpeed(long speed) {
                        taskInfo.setSpeed(speed + "kb/s");
                    }

                    @Override
                    public void setContentLength(long contentLength) {
                        taskInfo.setFileLength(String.valueOf(contentLength));
                    }

                    @Override
                    public void setBytes(byte[] bytes) {
                        taskInfo.setProgress(String.valueOf(Double.valueOf(bytes.length) / Long.parseLong(taskInfo.getFileLength())));
                        taskInfo.setOperation("下载视频......");
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        execute(() -> {
            try {
                HttpUtil.httpGetDownload(audioUrl, taskInfo.getRequestHeader().getHeaders(), taskInfo.getCachePath()+"/" + taskInfo.getName()+".m4a", 0, new HttpInfoListener() {

                    @Override
                    public void setSpeed(long speed) {
                        taskInfo.setSpeed(speed + "kb/s");
                    }

                    @Override
                    public void setContentLength(long contentLength) {
                        taskInfo.setFileLength(String.valueOf(contentLength));
                    }

                    @Override
                    public void setBytes(byte[] bytes) {
                        taskInfo.setProgress(String.valueOf(Double.valueOf(bytes.length) / Long.parseLong(taskInfo.getFileLength())));
                        taskInfo.setOperation("下载音频......");
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        while(true){
            if(this.getActiveCount()==0){
                break;
            }
        }
    }
}
